package servlet;

import dao.ShopDao;
import dao.UserDao;
import pojo.User;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.Objects;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public LoginServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("login.jpg");
        RequestDispatcher dispatcher = request.getRequestDispatcher("/YZMCodeServlet");
        UserDao dao = new UserDao();

        String account = request.getParameter("account");
        String password = request.getParameter("password");
        String yzm = request.getParameter("yzm");
        String auto = request.getParameter("auto");
        User user = dao.findOne(account);

        if (null == account || null == password || "".equals(account) || "".equals(password)) {
            session.setAttribute("login", "账号或密码不能为空！");
            requestDispatcher.forward(request, response);
            dispatcher.forward(request, response);
        } else if (!(Objects.equals(session.getAttribute("YZMCode"), yzm))) {
            session.setAttribute("login", "验证码错误！");
            requestDispatcher.forward(request, response);
            dispatcher.forward(request, response);
        } else if (user == null) {
            session.setAttribute("login", "用户不存在！");
            requestDispatcher.forward(request, response);
            dispatcher.forward(request, response);
        } else if (!(Objects.equals(user.getAccount(), account) && Objects.equals(user.getPassword(), password))) {
            session.setAttribute("login", "账号或密码错误！");
            requestDispatcher.forward(request, response);
            dispatcher.forward(request, response);
        } else {
            if (Objects.equals("auto", auto)) {  //自动登录
                String uri = request.getContextPath() + "/";  //存储路径
                int ecpiry = 60 * 60 * 24 * 7;  //存储时间7天
                Cookie c = new Cookie("account", user.getAccount());  //存储账号
                c.setPath(uri);
                c.setMaxAge(ecpiry);
                response.addCookie(c);
                c = new Cookie("password", user.getPassword());  //存储密码
                c.setPath(uri);
                c.setMaxAge(ecpiry);
                response.addCookie(c);
            }

            session.setAttribute("userName", user.getUsername());
            session.setAttribute("userJudge", "退出登录");
            session.setAttribute("user", user);
            request.getRequestDispatcher("index.jsp").forward(request,response);
        }
    }
}
